package com.neusoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.mapper.ApprovalRelateMapper;
import com.neusoft.mapper.DeptMapper;
import com.neusoft.mapper.EmpMapper;
import com.neusoft.model.ApprovalRelate;
import com.neusoft.model.Dept;
import com.neusoft.model.Emp;
import com.neusoft.service.ApprovalRelateService;
import com.neusoft.vo.ApprovalRelateVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApprovalRelateServiceServiceImpl implements ApprovalRelateService {
    @Resource
    private ApprovalRelateMapper approvalRelateMapper;
    @Override
    public IPage<ApprovalRelateVO> listApprovalRelates(String deptName, Integer pageNum, Integer pageSize) {
        Long total = approvalRelateMapper.selectApprovalRelatesCount(deptName);

        // 查询分页数据
        long offset = (long) (pageNum - 1) * pageSize;
        List<ApprovalRelateVO> records = approvalRelateMapper.selectApprovalRelatesWithNamesByPage(deptName, offset, pageSize);

        // 构造 IPage 对象
        IPage<ApprovalRelateVO> page = new Page<>(pageNum, pageSize, total);
        page.setRecords(records);

        return page;
    }
    @Resource
    private DeptMapper deptMapper;
    @Resource
    private EmpMapper empMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateApprover(ApprovalRelate approvalRelate) throws Exception {
        // 验证部门是否存在
        Dept dept = deptMapper.selectById(approvalRelate.getDeptId());
        if (dept == null) {
            throw new Exception("部门不存在");
        }
        // 验证审批人是否为该部门的员工
        if (isEmployeeInDepartment(approvalRelate.getAttendanceApproverId(), approvalRelate.getDeptId()) ||
                isEmployeeInDepartment(approvalRelate.getVocationApproverId(), approvalRelate.getDeptId())) {
            throw new Exception("审批人必须是部门内的员工");
        }
        // 检查审批人是否已在其他部门担任审批人
        if (isApproverInOtherDepartment(approvalRelate.getAttendanceApproverId(), approvalRelate.getDeptId()) ||
                isApproverInOtherDepartment(approvalRelate.getVocationApproverId(), approvalRelate.getDeptId())) {
            throw new Exception("审批人已在其他部门担任审批人职务");
        }
        // 更新 approval_relate 表
        UpdateWrapper<ApprovalRelate> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("dept_id", approvalRelate.getDeptId());
        boolean approvalRelateUpdated = approvalRelateMapper.update(approvalRelate, updateWrapper) > 0;
        // 更新 dept 表
        dept.setTimeKeeper(approvalRelate.getAttendanceApproverId().toString());
        dept.setLeaveApprover(approvalRelate.getVocationApproverId().toString());
        boolean deptUpdated = deptMapper.updateById(dept) > 0;
        return approvalRelateUpdated && deptUpdated ? "更新成功" : "更新失败";
    }
    private boolean isEmployeeInDepartment(Integer empId, Integer deptId) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", empId).eq("dept_id", deptId);
        return empMapper.selectCount(queryWrapper) <= 0;
    }
    private boolean isApproverInOtherDepartment(Integer empId, Integer currentDeptId) {
        QueryWrapper<ApprovalRelate> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("dept_id", currentDeptId)
                .and(wrapper -> wrapper.eq("attendance_approver_id", empId)
                        .or()
                        .eq("vocation_approver_id", empId));
        return approvalRelateMapper.selectCount(queryWrapper) > 0;
    }
}
